package net.sunzc.takephoto;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
import net.sunzc.hwfacetest.R;
import net.sunzc.takephoto.utils.MyLog;

@SuppressLint("NewApi")
public class TAddFaceDelog extends PhotoActivity<ImageItem> implements FaceCollector.CollectionListener {

    public static final String IC_CARD = "jlsfz";
    private static final String FACE_DATA = "faceData";
    private String mIcValue; // IC卡信息
    private FaceCollector mCollector;

    public static void openActivity(Activity context, String icCard, int requestId) {
        Intent intent = new Intent(context, TAddFaceDelog.class);
        intent.putExtra(IC_CARD, icCard);
        context.startActivityForResult(intent, requestId);
    }

    public static String getFaceData(int result, Intent data) {
        if (result == RESULT_OK) {
            return data.getStringExtra(FACE_DATA);
        }
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_face_delog);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制竖屏
        Intent intent = getIntent();
        if (intent != null) {
            mIcValue = intent.getStringExtra(IC_CARD);
        }
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        initCollector(surfaceView);
    }

    private void initCollector(SurfaceView surfaceView) {
        ICamera camera = MyLog.getCamera(this, surfaceView);
        mCollector = new FaceCollector(TAddFaceDelog.this, camera, mIcValue);
        mCollector.setCollectListener(this);
        mCollector.collectFace();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCollector.exit();
    }

    @Override
    public void onExit() {
    }

    @Override
    public void onCollect(int count, Bitmap bitmap) {
        // 更新进度
        getAdapter().add(new ImageItem(bitmap));
    }

    @Override
    public void onFinish(String feature) {
        Intent data = new Intent();
        data.putExtra(FACE_DATA, feature);
        setResult(RESULT_OK, data);
        finish();
    }
}
